﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Diagnostics;
using System.ComponentModel;
using System.IO;
using System.Windows.Threading;

namespace homework1_auto_ques.pages
{
    /// <summary>
    /// Page2.xaml 的交互逻辑
    /// </summary>
    /// 

    public partial class Page2 : INotifyPropertyChanged
    {
        //数据绑定
        public event PropertyChangedEventHandler PropertyChanged;

        //视图数据
        private int _leftQuesNum;
        public int LeftQuesNum
        {
            set
            {
                _leftQuesNum = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("LeftQuesNum"));
                }
            }
            get
            {
                return _leftQuesNum;
            }
        }
        private int _rightQuesNum;
        public int RightQuesNum
        {
            set
            {
                _rightQuesNum = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("RightQuesNum"));
                }
            }
            get
            {
                return _rightQuesNum;
            }
        }
        private int _wrongQuesNum;
        public int WrongQuesNum
        {
            set
            {
                _wrongQuesNum = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("WrongQuesNum"));
                }
            }
            get
            {
                return _wrongQuesNum;
            }
        }
        private int _totalTime;
        public int TotalTime
        {
            set
            {
                _totalTime = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("TotalTime"));
                }
            }
            get
            {
                return _totalTime;
            }
        }
        private int _totalScore;
        public int TotalScore
        {
            set
            {
                _totalScore = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("TotalScore"));
                }
            }
            get
            {
                return _totalScore;
            }
        }
        private string _screenMode;
        public string ScreenMode
        {
            set
            {
                _screenMode = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("ScreenMode"));
                }
            }
            get
            {
                return _screenMode;
            }
        }
        private string _quesText;
        public string QuesText
        {
            set
            {
                _quesText = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("QuesText"));
                }
            }
            get
            {
                return _quesText;
            }
        }
        private int _quesTime;
        public int QuesTime
        {
            set
            {
                _quesTime = value;
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged.Invoke(this, new PropertyChangedEventArgs("QuesTime"));
                }
            }
            get
            {
                return _quesTime;
            }
        }

        //单词
        List<WordPair> wordPairs;

        //逻辑数据
        private bool running;//运行或暂停
        private bool enableAnswer;//未显示提示，允许答题
        private int quesMode;//问题模式，0为计算，1为背单词
        private string quesAnswer;//当前问题的答案
        private int quesDur;//每道题的限时
        private int point;//每道题的分数
        DispatcherTimer timer = new DispatcherTimer();//计时器
        Random ran = new Random();//随机数生成器


        //构造函数
        public Page2(int quesTime, int quesNum, int mode)
        {
            InitializeComponent();
            wordPairs = ReadWordPairsFromFile("wordlist.txt");
            InitPage(quesTime, quesNum, mode);
            DataContext = this;
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += Timer_Tick;
            timer.Start();
        }

        //每秒执行
        private void Timer_Tick(object sender, EventArgs e)
        {
            if (QuesTime > 0 && LeftQuesNum > 0)
            {
                QuesTime -= 1;
                TotalTime += 1;
            }
            else if(QuesTime == 0 && LeftQuesNum > 0)
            {
                if (enableAnswer)
                {
                    //超时回答错误
                    AnswerWrong();
                }
                else
                {
                    enableAnswer = true;
                    QuesTime = quesDur;
                    QuesText = getNewQues();
                    answerTextBox.Text = null;
                }
            }
            else
            {
                running = false;
                pauseBtn.Content = "继续";
                timer.Stop();
                QuesText = "已全部完成，你的得分是：" + TotalScore;
                pauseBtn.Content = "返回";
            }
            
        }

        //读取单词文件
        static List<WordPair> ReadWordPairsFromFile(string filePath)
        {
            List<WordPair> wordPairs = new List<WordPair>();

            try
            {
                using (StreamReader reader = new StreamReader(filePath))
                {
                    string line;
                    while ((line = reader.ReadLine()) != null)
                    {
                        // 假设文件中每行的格式是 "英文单词 汉语翻译"
                        string[] parts = line.Split(' ');
                        if (parts.Length == 2)
                        {
                            WordPair pair = new WordPair
                            {
                                EnglishWord = parts[0],
                                ChineseTranslation = parts[1]
                            };
                            wordPairs.Add(pair);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"读取文件时出错: {ex.Message}");
            }

            return wordPairs;
        }

        //获取问题和问题答案
        private string getNewQues()
        {
            switch (quesMode)
            {
                case 0:
                    {
                        //获取随机的算术题
                        int a = ran.Next(100);
                        int b = ran.Next(100);
                        string cop;
                        if (a%2 == 0)
                        {
                            quesAnswer = (a + b).ToString();
                            cop = "+";
                        }
                        else
                        {
                            quesAnswer =(a - b).ToString();
                            cop = "-";
                        }
                        return a + cop + b;
                    }
                case 1:
                    {
                        //获取单词题
                        quesAnswer = wordPairs[RightQuesNum+WrongQuesNum].EnglishWord;
                        return wordPairs[RightQuesNum + WrongQuesNum].ChineseTranslation;
                    }
                default: return "未知错误";
            }
        }

        //初始化设置视图数据
        private void InitPage(int quesTime, int quesNum, int mode)
        {
            switch (mode)
            {
                case 0:
                    ScreenMode = "算数";
                    quesMode = 0;
                    break;
                case 1:
                    ScreenMode = "背单词";
                    quesMode = 1;
                    break;
            }
            LeftQuesNum = quesNum;
            quesDur = quesTime;
            QuesTime = quesDur;
            RightQuesNum = 0;
            WrongQuesNum = 0;
            TotalTime = 0;
            TotalScore = 0;
            QuesText = getNewQues();
            running = true;
            enableAnswer = true;
            point = 100 / quesNum;
        }

        //回答错误
        private void AnswerWrong()
        {
            enableAnswer = false;
            QuesText = "回答错误，正确答案："+quesAnswer;
            QuesTime = 3;
            WrongQuesNum += 1;
            LeftQuesNum -= 1;
        }

        //回答正确
        private void AnswerRight()
        {
            enableAnswer = false ;
            QuesText = "回答正确：" + quesAnswer;
            QuesTime = 1;
            RightQuesNum += 1;
            LeftQuesNum -= 1;
            TotalScore += point;
        }

        //确定按钮
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (running)
            {
                if (enableAnswer)
                {
                    if (quesAnswer == answerTextBox.Text)
                    {
                        AnswerRight();
                    }
                    else
                    {
                        AnswerWrong();
                    }
                }
                else {
                    MessageBox.Show("请等待下一题");
                }
            }
            else
            {
                if (LeftQuesNum>0)
                {
                    MessageBox.Show("请点击继续");
                }
                else
                {
                    MessageBox.Show("请返回并重新开始");
                }
            }
        }

        //暂停按钮
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            if (LeftQuesNum == 0)
            {
                //返回上一个页面
                Frame frame1 = new Frame()
                {
                    Content = new pages.Page1()
                };
                MainWindow.win.ContentCon.Content = frame1;
            }
            else
            {
                if (running)
                {
                    running = false;
                    pauseBtn.Content = "继续";
                    timer.Stop();
                }
                else
                {
                    running = true;
                    pauseBtn.Content = "暂停";
                    timer.Start();
                }
            }
        }

        private void Image_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            MainWindow.win.Close();
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {

        }
    }

    //单词组
    public class WordPair
    {
        public string EnglishWord { get; set; }
        public string ChineseTranslation { get; set; }
    }
}
